import { requestClient } from '#/api/request';

export namespace AuthApi {
  /** 请求获取验证码 */
  export interface CapJsonParams {
    sumid: string;
  }

  export interface CapJsonResult {
    _cap: string;
    image: string;
    retCode: number;
  }

  /** 请求验证验证码 */
  export interface CapCheckParams {
    sumid: string;
    vcode: string;
  }

  export interface CapCheckResult {
    retCode: number;
    xtoken: string;
  }

  /** 登录接口参数 */
  export interface LoginParams {
    password: string;
    token: string;
    username: string;
  }

  export interface AccessCodeResult {
    access: string[];
    retCode: number;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    accessToken: string;
    desc: string;
    realName: string;
    refreshToken: string;
    retCode: number;
    userId: string;
    username: string;
  }
}

/**
 * 登录
 */
export async function login(data: AuthApi.LoginParams) {
  if (!data.token) {
    data.token = String(Date.now() % 10 ** 6);
  }
  return requestClient.post<AuthApi.LoginResult>('/sys/signIn/check', data);
}

/**
 * 请求获取验证码
 */
export async function capJson(data: AuthApi.CapJsonParams) {
  return requestClient.post<AuthApi.CapJsonResult>('/sys/captcha/json', data);
}

/**
 * 请求获取验证码
 */
export async function capCheck(data: AuthApi.CapCheckParams) {
  return requestClient.post<AuthApi.CapCheckResult>('/sys/captcha/check', data);
}

/**
 * 获取用户权限码
 */
export async function getAccessCodes() {
  return requestClient.post<AuthApi.AccessCodeResult>('/sys/access/codes');
}
